package com.googlecode.animeclassifier.attribute;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class RuleReader {
	public static String DEFAULT_RULE_FILENAME = "default.rule";
	
	public static List<Rule> getRule(String filename) {
		BufferedReader reader;
		List<Rule> ret = new LinkedList<Rule>();
		
		try {
			reader = new BufferedReader(new FileReader(filename));
			String line = "";
			int lineNumber = 0;
			
			try {
				while((line = reader.readLine()) != null) {
					String[] cols;
					cols = line.split(",");
					
					if(cols.length == 2) {
						ret.add(new Rule(cols[0], Attributes.valueOf(cols[1]), null));
					} else if(cols.length == 3){
						ret.add(new Rule(cols[0], Attributes.valueOf(cols[1]), cols[2]));
					} else {
						System.err.println("Line " + lineNumber + ": Error");
					} 
					
					lineNumber++;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		
		System.out.println(ret);
		return ret;
	}
	
	public static List<Rule> getRule() {
		System.out.println("Into getRule()");
		return getRule(DEFAULT_RULE_FILENAME);
	}
}
